package com.sfang;

import com.sfang.common.util.encrypt.RSAUtil;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {


        String publicKeyBase64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxyu4TzkYSTDYdQtxurRhfCpNbLmiHpCqxBUhhBrZ9KzIy35rRv5wC5xhz0LJznRStCzcojHWKK4YxmBDDDScB6qf5buG0kyNhIiNhtRdj3ep5MHPyXT84jqBDxVt8jexRCuVWrl71LFIidGIYEzG4328ThJMZo0bCG43+g6sJs7EBkGhiZCb3BZSUBzyXNQQYGzkCcVAN2PRaqSHF3u+L0KB6Ocw9BuYBZ/dp+OmS9cOD08bpzI3yZ2dpj5bCdntVP5s7gVOh1g8b/pUd5UZJkc0zRTFlZ+7vdFp3uYvpJ8kTlSzn6liKJDYo3EPlRmHuH2LioarN1DEAYmcZZlgwQIDAQAB";
        String privateKeyBase64 = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHK7hPORhJMNh1C3G6tGF8Kk1suaIekKrEFSGEGtn0rMjLfmtG/nALnGHPQsnOdFK0LNyiMdYorhjGYEMMNJwHqp/lu4bSTI2EiI2G1F2Pd6nkwc/JdPziOoEPFW3yN7FEK5VauXvUsUiJ0YhgTMbjfbxOEkxmjRsIbjf6DqwmzsQGQaGJkJvcFlJQHPJc1BBgbOQJxUA3Y9FqpIcXe74vQoHo5zD0G5gFn92n46ZL1w4PTxunMjfJnZ2mPlsJ2e1U/mzuBU6HWDxv+lR3lRkmRzTNFMWVn7u90Wne5i+knyROVLOfqW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vNBpyRn95KYDFihqnyReSVII4F+s1AoGAGE6eU+83ZPbNKu/c6wO6rXka8lS9Msa6DI7toqZmOqp8b3U3CYnSL1yAH1n9e/8u2KToKHi+2mbMA5gGierCdPiQEyfjYFjIzNuGdZQcHkW67mFSvyeiBrzVO5BJY2Odq6wEKhthrnI+j7RG0LqCBefeKxNhz741d4/4WGpLvnE=";
        PublicKey publicKey = KeyFactory.getInstance("RSA")
                .generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyBase64)));
        PrivateKey privateKey = KeyFactory.getInstance("RSA")
                .generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyBase64)));

        byte[] encryptedBytes = RSAUtil.encrypt("hello", publicKey);
        byte[] plainText = RSAUtil.decrypt(encryptedBytes, privateKey);

        String decryptedText = new String(plainText);
        System.out.println("加密后的数据: " + Base64.getEncoder().encodeToString(encryptedBytes));
        System.out.println("解密后的数据: " + decryptedText);
    }
}
